#pragma region Inclue
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <stdlib.h>

#include "debug.h"

#include "web_content.h"
#include "charset_codes.h"
#include "MIME-types.h"
#include "other.h"
#pragma endregion Include

char *CWebContent::get_content(const char *path, char *buf)
{
	printf("open file %s", path);
	FILE *fp = NULL;

	fp = fopen(path,"rb");
	if(!fp)
		return NULL;

	int cnt = 0;

	while(!feof(fp))
	{
		buf[cnt] = (char)fgetc(fp);
		cnt++;
	}

	fclose(fp);
	printf("close file %s", path);
}

long CWebContent::get_size(const char *path)
{
	FILE *fp = NULL;
	int fd = 0;
	long file_size = 0;

	fp = fopen(path,"rb");
	if(!fp)
		return 0;

	fd = fileno(fp);
	file_size = filelength(fd) + 1;

	fclose(fp);

	return file_size;
}

char *CWebContent::get_content_type(const char *path)
{
	char *filename = new char [_MAX_FNAME];
	char *ext = new char [_MAX_EXT];
	char *result = 0;

	_splitpath(path, NULL, NULL, filename, ext);

	if(strcmp(".png", ext) == 0)
		result = IMAGE_PNG;
	else if(strcmp(".jpg", ext) == 0)
		result = IMAGE_JPEG;
	else if(strcmp(".gif", ext) == 0)
		result = IMAGE_GIF;
	else if(strcmp(".html", ext) == 0)
		result = TEXT_HTML;
	else if(strcmp(".css", ext) == 0)
		result = TEXT_CSS;
	else
		result = UNKNOWN_TYPE;

	delete [] filename;
	delete [] ext;

	return result;
}